<?php
// ***************************************************************************** 
//  Copyright 2008 AMC - Websource
//  Glenview, Illinois 
// 
//  File:           class_session.php 
//  Author:         Mickey Freeman (mfreeman@connect2amc.com)
//  Function:       Class definition for conference session class
// 
//  Created: 10/05/2008
//  Version: 1.0 
// 
//  Modified: 
//    
// 
// ******************************************************************************

class session extends app_object 
{
    function session()
    {
        $fields = array('session_code',
                        'session_start_date',
                        'session_end_date',
                        'session_room');
                        
        $table = SESSION_TABLE;
        $id_field = 'session_id';
        
        parent::app_object($fields, $table, $id_field);
    }
    
    function save()
    {
        if (!is_numeric($this->values['session_start_date'])) {
            $this->values['session_start_date'] = strtotime($this->values['session_start_date']);
        }
        
        if (!is_numeric($this->values['session_end_date'])) {
            $this->values['session_end_date'] = strtotime($this->values['session_end_date']);
        }
        
        parent::save();
    }
    
    function load($session_id)
    {
        parent::load($session_id);
        
        if ($this->values['session_start_date'] != 0) {
            $session_date = $this->values['session_start_date'];
            $this->values['session_start_date'] = date('m/d/Y', $session_date);
            $this->values['session_start_time'] = date('h:i a', $session_date);
            $this->values['session_start_time_hours'] = date('h', $session_date);
            $this->values['session_start_time_minutes'] = date('i', $session_date);
            $this->values['session_start_time_post']    = date('a', $session_date);
        }        
        if ($this->values['session_end_date'] != 0) {
            $session_date = $this->values['session_end_date'];
            $this->values['session_end_date'] = date('m/d/Y', $session_date);
            $this->values['session_end_time'] = date('h:i a', $session_date);
            $this->values['session_end_time_hours'] = date('h', $session_date);
            $this->values['session_end_time_minutes'] = date('i', $session_date);
            $this->values['session_end_time_post']    = date('a', $session_date);
        }
    }
    
    function load_from_request() 
    {
        parent::load_from_request();
        
        $this->values['session_start_time_hours'] = $_REQUEST['session_start_time_hours'];
        $this->values['session_start_time_minutes'] = $_REQUEST['session_start_time_minutes'];
        $this->values['session_start_time_post'] = $_REQUEST['session_start_time_post'];
        
        $this->values['session_start_date'] .= " " . $this->values['session_start_time_hours'] . ":" 
                                      .  $this->values['session_start_time_minutes'] . " " . $this->values['session_start_time_post'];
                                              
        $this->values['session_end_time_hours'] = $_REQUEST['session_end_time_hours'];
        $this->values['session_end_time_minutes'] = $_REQUEST['session_end_time_minutes'];
        $this->values['session_end_time_post'] = $_REQUEST['session_end_time_post'];
        
        $this->values['session_end_date'] .= " " . $this->values['session_end_time_hours'] . ":" 
                                      .  $this->values['session_end_time_minutes'] . " " . $this->values['session_end_time_post'];
                                      

    }
    
    function get_sessions()
    {
        $sql = "SELECT * FROM " . SESSION_TABLE;
        
        $result = app_data::mysql_query($sql);
        
        $sessions = array();
        
        while ($row = mysql_fetch_assoc($result)) {
            $sessions[] = $row;
        }
        
        return $sessions;
    }
    
    function get_sessions_for_schedule() 
    {
        $sql = "SELECT * FROM " . SESSION_TABLE . " ORDER BY session_start_date ASC, session_code ASC";
        
        $result = app_data::mysql_query($sql);
        
        $sessions = array();
        
        while($row = mysql_fetch_array($result)) {
            $sessions[$row['session_id']] = $row['session_code'] . ": " . date('m/d h:i a', $row['session_start_date']) . " - "
                                          . date('m/d h:i a', $row['session_end_date']) . ", " . $row['session_room']; 
        }
        
        return $sessions;
    }
    
    function validate_session()
    {
        $errmsg = array();
        
        if ($this->values['session_code'] == '') {
            $errmsg[] = "Session Code has not been specified!";
        }
        
        if ($this->values['session_start_date'] == '' || $this->values['session_start_date'] == 0) {
            $errmsg[] = "Session start date & time has not been specified!";
        } else {
            if (!strtotime($this->values['session_start_date'])) {
                $errmsg[] = "An invalid session start date & time has been entered!";
            }
        }        
        if ($this->values['session_end_date'] == '' || $this->values['session_end_date'] == 0) {
            $errmsg[] = "Session ending date & time has not been specified!";
        } else {
            if (!strtotime($this->values['session_end_date'])) {
                $errmsg[] = "An invalid session ending date & time has been entered!";
            }
        }
        
        if ($this->values['session_room'] == '') {
            $errmsg[] = "Session room has not been specified!";
        }
            
        return $errmsg;
    }
}